<?php
abstract class STE_Media extends STE_Db
{
	const MEDIA_TEMP_DIRECTORY = "/home/vhosts/steel/www/x/temp/";
	const MEDIA_DIRECTORY = "/home/vhosts/steel/www/x/media/";
	const MEDIA_URL_PREFIX = "/x/media/";
	
	/**
	@param photoFilePath Photo file path
	@param $fieldNameWithoutExtension For example: 'test' instead of 'test.jpg'
	@return array
	**/
	protected function _photoResize($photoFilePath, $fielNameWithoutExtension)
	{
		$fullpath = $photoFilePath;
		$imageType = exif_imagetype($fullpath);
		$originalImage = 0;
		switch ($imageType)
		{
			case IMAGETYPE_GIF:
				$originalImage = imagecreatefromgif($fullpath);
				break;
			case IMAGETYPE_JPEG:
				$originalImage = imagecreatefromjpeg($fullpath);
				break;
			case IMAGETYPE_PNG:
				$originalImage = imagecreatefrompng($fullpath);
				break;
		}
		// Calculate dimension
		list($imageWidth, $imageHeight) = getimagesize($fullpath);
		
		$realWidth = 0;
		$realHeight = 0;
		$thumbnailWidth = 0;
		$thumbnailHeight = 0;
		if ($imageWidth <= $imageHeight)
		{
			$realHeight = 550; // 150 hoac 550 tuy ngu canh
			$realWidth = ceil(($realHeight / $imageHeight) * $imageWidth);
			$thumbnailHeight = 150;
			$thumbnailWidth = ceil(($thumbnailHeight / $imageHeight) * $imageWidth);
		}
		else
		{
			$realWidth = 550; // 150 hoac 550 tuy ngu canh
			$realHeight = ceil(($realWidth / $imageWidth) * $imageHeight);
			$thumbnailWidth = 150;
			$thumbnailHeight = ceil(($thumbnailWidth / $imageWidth) * $imageHeight);			
		}
		
		// Resize cover
		$realImage = imagecreatetruecolor($realWidth, $realHeight);
		$thumbnailImage = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight);
		imagecopyresampled($realImage, $originalImage, 0, 0, 0, 0, $realWidth, $realHeight, $imageWidth, $imageHeight);
		imagecopyresampled($thumbnailImage, $originalImage, 0, 0, 0, 0, $thumbnailWidth, $thumbnailHeight, $imageWidth, $imageHeight);
				
		// Save to disk
		switch ($imageType)
		{
			case IMAGETYPE_GIF:
				$realFilePath = self::MEDIA_TEMP_DIRECTORY . $fielNameWithoutExtension . "_real.gif";
				imagegif($realImage, $realFilePath);
				$thumbnailFilePath = self::MEDIA_TEMP_DIRECTORY . $fielNameWithoutExtension . "_thumbnail.gif";
				imagegif($thumbnailImage, $thumbnailFilePath);
				break;
			case IMAGETYPE_JPEG:
				$realFilePath = self::MEDIA_TEMP_DIRECTORY . $fielNameWithoutExtension . "_real.jpg";
				imagejpeg($realImage, $realFilePath, 90);
				$thumbnailFilePath = self::MEDIA_TEMP_DIRECTORY . $fielNameWithoutExtension . "_thumbnail.jpg";
				imagejpeg($thumbnailImage, $thumbnailFilePath, 90);
				break;
			case IMAGETYPE_PNG:
				$realFilePath = self::MEDIA_TEMP_DIRECTORY . $fielNameWithoutExtension . "_real.png";
				imagepng($realImage, $realFilePath, 9);
				$thumbnailFilePath = self::MEDIA_TEMP_DIRECTORY . $fielNameWithoutExtension . "_thumbnail.png";
				imagepng($thumbnailImage, $thumbnailFilePath, 9);
				break;
		}
		
		// Clean up
		imagedestroy($realImage);
		imagedestroy($thumbnailImage);
		
		return array('realFilePath' => $realFilePath, 
					 'thumbnailFilePath' => $thumbnailFilePath,
					 'realWidth' => $realWidth,
					 'realHeight' => $realHeight,
					 'thumbnailWidth' => $thumbnailWidth,
					 'thumbnailHeight' => $thumbnailHeight);
	}
	
	/**
	@param $fileName string File name with form 'test.jpg'
	@return array
	**/
	protected function _moveTempFileToMediaFolder($fileName)
	{
		// We get only 'test' from 'test.jpg'.
		$fileNameWithoutExtension = substr($fileName, 0, strrpos($fileName, '.'));		
		
		// get original file path '/home/vhosts/steel/www/x/temp/test.jpg'.
		$filePath = self::MEDIA_TEMP_DIRECTORY . $fileName;
		
		// Resize 'test.jpg' to a 'test_real.jpg' and 'test_thumnail.jpg'
		$a = $this->_photoResize($filePath, $fileNameWithoutExtension);
		
		// Prepare info array.
		$info = $this->_prepareInfoArray($a);		
		
		// Delete file 'test.jpg'.
		$this->_deleteTempFile($fileName);
		
		// Move 'test_medium.jpg' to '/home/vhosts/steel/www/x/media'.
		$successful1 = rename($info['realFilePath'], self::MEDIA_DIRECTORY . $info['realFileName']);
		$successful2 = rename($info['thumbnailFilePath'], self::MEDIA_DIRECTORY . $info['thumbnailFileName']);
		if($successful1 && $successful2) {
			$info['successful'] = true;
			return $info;
		}
		
		// Delete 'test_real.jpg' and 'test_thumbnail' if the above if statement not successful.
		$this->_deleteTempFile($info['realFileName']);
		$this->_deleteTempFile($info['thumbnailFileName']);		
		$info['successful'] = false;
		return $info;
	}
	
	private function _prepareInfoArray($a)
	{		
		$infoArray = $a;
		
		// Get path info of real file and thumbnail file
		$realPathInfo = pathinfo($infoArray['realFilePath']);
		$thumbnailPathInfo = pathinfo($infoArray['thumbnailFilePath']);		
		
		// Store file name to return array
		$infoArray['realFileName'] = $realPathInfo['basename'];
		$infoArray['thumbnailFileName'] = $thumbnailPathInfo['basename'];		
		
		return $infoArray;
	}
	
	private function _deleteTempFile($fileName)
	{
		if (file_exists(self::MEDIA_TEMP_DIRECTORY . $fileName))
			return unlink(self::MEDIA_TEMP_DIRECTORY . $fileName);
		return false;
	}
	
	protected function _deleteMediaFile($fileName)
	{		
		if (file_exists(self::MEDIA_DIRECTORY . $fileName))
			return unlink(self::MEDIA_DIRECTORY . $fileName);
		return false;
	}

	protected function _getFileNameFromFilePath($filePath)
	{
		return substr($filePath, strrpos($filePath, '/') + 1);
	}	
}	
?>